package NG::Gen::Task::BuyUnit;
use strict;
use warnings;

use NG::Sys::App;

use NG::Sys::Datetime;
our @ISA = qw(NG::Gen::Task::Task);

sub delay {
	my $self = shift;
	my $p = $self->param;
	return $p->{delay_base} + $p->{delay_per} * $p->{count};
}

sub before_assign {
	my $self = shift;
	if($self->{user}->u_point < $self->param->{count}){
		return "Недостаточно средств";
	}
	my %avail = map {$_=>1} split /,/, $self->param->{types};
	unless($avail{$self->param->{type}}){
		return "Это не доступно";
	}
	return 0;
}
sub after_assign {
	my $self = shift;
	$self->{user}->u_point($self->{user}->u_point - $self->param->{count});
	return 0;
}
sub before_done {
	my $self = shift;
	my $p = $self->param;
	my $unit = app()->reference("units")->get_by_key($p->{type});
	$self->{user}->add_unit($self->{leader},$unit,$p->{count});
	return 0;
}
sub after_cancel {
	my $self = shift;
	$self->{user}->u_point($self->{user}->u_point + $self->param->{count});
	return 0;
}

1;
